package com.gitee.srurl.core.domain;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.gitee.srurl.core.util.valid.AddGroup;
import com.gitee.srurl.core.util.valid.UpdateGroup;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author zxw
 * @date 2023/5/2 22:23
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("sus_user")
public class SusUserDomain implements Serializable {

    @Serial
    private static final long serialVersionUID = -3993790375364588796L;

    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 昵称
     */
    @NotBlank(message = "昵称不能为空！", groups = {AddGroup.class, UpdateGroup.class})
    private String nickname;

    /**
     * 用户名
     * 不允许更新
     */
    @NotBlank(message = "用户名不能为空！", groups = {AddGroup.class})
    private String username;

    /**
     * 密码
     * 只在序列化的时候忽略 反序列化不忽略
     */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @NotBlank(message = "密码不能为空！", groups = {AddGroup.class})
    private String password;

    /**
     * 状态 (0:禁用1:正常)
     */
    @NotBlank(message = "状态不能为空！", groups = {AddGroup.class, UpdateGroup.class})
    @Pattern(message = "状态参数错误！", regexp = "^0|1$", groups = {AddGroup.class, UpdateGroup.class})
    private String status;

    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    /**
     * 创建ID
     */
    @TableField(fill = FieldFill.INSERT)
    private Long createId;

    /**
     * 更新时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    /**
     * 更新ID
     */
    @TableField(fill = FieldFill.INSERT)
    private Long updateId;

    /**
     * api用户 (0:否1:是)
     * 不允许更新
     */
    @Pattern(message = "状态参数错误！", regexp = "^0|1$", groups = {AddGroup.class})
    @NotBlank(message = "状态不能为空！", groups = {AddGroup.class})
    private String apiUser;

    /**
     * api用户使用登录key
     * 不允许更新
     */
    private String apiKey;

    /**
     * 最大允许url长度（api用户使用）
     */
    @Min(value = -1, message = "最大URL长度不能小于-1", groups = {UpdateGroup.class, AddGroup.class})
    @Max(value = 2048, message = "最大URL长度不能大于2048", groups = {UpdateGroup.class, AddGroup.class})
    @TableField(exist = false)
    private Integer allowUrlLength;
}
